package com.cloud.upms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.cloud.common.core.result.Result;
import com.cloud.common.log.annotation.SysLog;
import com.cloud.common.log.enums.OperateTypeEnum;
import com.cloud.upms.service.SysDeptService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.cloud.api.upms.domain.dept.DeptAddUpdateReq;

import java.util.List;

/**
 * Author:  xuanning
 * Date:    2023/2/9 16:33
 */
@RestController
@AllArgsConstructor
public class SysDeptController {

    private final SysDeptService sysDeptService;

    /**
     * 新增
     * @return
     */
    @PostMapping(value = "/dept/add")
    @SaCheckPermission(value = "dept:add", orRole = "admin")
    @SysLog(title = "部门管理",operate = OperateTypeEnum.ADD)
    public Result<?> add(@Valid @RequestBody DeptAddUpdateReq paramReq) {
        sysDeptService.addOrUpdate(paramReq);
        return Result.success();
    }

    /**
     * 修改
     * @return
     */
    @PostMapping(value = "/dept/update")
    @SaCheckPermission(value = "dept:update", orRole = "admin")
    @SysLog(title = "部门管理",operate = OperateTypeEnum.UPDATE)
    public Result<?> update(@Valid @RequestBody DeptAddUpdateReq paramReq) {
        sysDeptService.addOrUpdate(paramReq);
        return Result.success();
    }


    /**
     * 删除
     * @return
     */
    @GetMapping(value = "/dept/delete")
    @SaCheckPermission(value = "dept:delete", orRole = "admin")
    @SysLog(title = "部门管理",operate = OperateTypeEnum.DELETE)
    public Result<?> delete(Long id) {
        sysDeptService.delete(id);
        return Result.success();
    }

    /**
     * 列表
     * @return
     */
    @GetMapping(value = "/dept/list")
    @SaCheckPermission(value = "dept", orRole = "admin")
    @SysLog(title = "部门管理",operate = OperateTypeEnum.QUERY)
    public Result<?> listDept() {
        List<Tree<Long>> tree= sysDeptService.listDept();
        return Result.success(tree);
    }


    /**
     * 用户树
     * @return
     */
    @GetMapping(value = "/dept/user-list")
    public Result<?> listUserDept() {
        List<Tree<Long>> tree= sysDeptService.listUserDept();
        return Result.success(tree);
    }
}
